package com.example.demo;

import org.docx4j.Docx4J;
import org.docx4j.Docx4jProperties;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.convert.out.HTMLSettings;
import org.docx4j.convert.out.html.SdtToListSdtTagHandler;
import org.docx4j.convert.out.html.SdtWriter;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFont;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.P;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * docx4j 将word转换为html
 */
public class Docx4jConvert2Html {
    static final String DIR_PATH="/Users/wuxinbo/poiDemo/";
      /**
       * 输出的docx文件路径
       */
    static final String OUTPUT_DOCX_PATH=DIR_PATH+"04.docx";
    static Logger logger = LoggerFactory.getLogger(Docx4jConvert2Html.class);
    public static void main(String args[]) throws Exception {
        WordprocessingMLPackage mlPackage =WordprocessingMLPackage.load(new File(OUTPUT_DOCX_PATH));
//        MainDocumentPart mainDocumentPart = mlPackage.getMainDocumentPart();
//        List<Object> content = mainDocumentPart.getContent();

        String userCSS = "html, body, div, span, h1, h2, h3, h4, h5, h6, p, a, img,  table, caption, tbody, tfoot, thead, tr, th, td " +
                "{ margin: 0; padding: 0; border: 0;}" +
                "body {line-height: 1;} ";
        HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
//        htmlSettings.setUserCSS(userCSS);
        SdtWriter.registerTagHandler("HTML_ELEMENT",new SdtToListSdtTagHandler());
        htmlSettings.setImageDirPath(DIR_PATH);
        htmlSettings.setWmlPackage(mlPackage);
        convetToPDF(mlPackage);
        Docx4J.toHTML(htmlSettings,new FileOutputStream(new File(DIR_PATH+"test.html")),Docx4J.FLAG_EXPORT_PREFER_XSL);
    }

    /**
     * 导出为pdf文档
     * @param mlPackage
     */
    static void convetToPDF(WordprocessingMLPackage mlPackage) throws Exception {
//        Mapper mapper =new IdentityPlusMapper();
        Docx4jProperties.setProperty("com.plutext.converter.URL","http://aliyun:9016/v1/00000000-0000-0000-0000-000000000000/convert");
//        mlPackage.setFontMapper(mapper);
//        PhysicalFonts.discoverPhysicalFonts();
//        PhysicalFonts.getPhysicalFonts().forEach((key,value)->
//        {
//            logger.debug("fontKey is "+key);
//        }
//        );
//        PhysicalFonts.setRegex(null);
//        PhysicalFont font = PhysicalFonts.get("SimSun");
//        mapper.put("宋体",font);
//        mapper.put("黑体",PhysicalFonts.get("microsoft sans serif"));
//        mapper.put("仿宋",PhysicalFonts.get("FangSong"));
//        mapper.put("楷体_GB2312",font);
//        FOSettings foSettings = Docx4J.createFOSettings();
//        foSettings.setWmlPackage(mlPackage);
        FileOutputStream fileOutputStream = new FileOutputStream(DIR_PATH + "test.pdf");
        Docx4J.toPDF(mlPackage,fileOutputStream);
//        Docx4J.toFO(foSettings,fileOutputStream,Docx4J.FLAG_EXPORT_PREFER_XSL);
    }
}
